using System;

namespace Googler.BLL
{
	/// <summary>
	/// Summary description for InfoBase.
	/// </summary>
	public  abstract class InfoBase:IListItem
	{
		public LayerType Layer;

		public void StartDAL()
		{
			Layer=LayerType.DAL;
		}
		public void EndDAL()
		{
			Layer=LayerType.PLL;
		}
		public static object	GetDate(DateTime date,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.PLL:
					return date==DateTime.MinValue?(object)"":(object)((DateTime)date).ToShortDateString();
					
				case LayerType.DAL:
					return date==DateTime.MinValue?(object)DBNull.Value:(object)date;

				default:
					return date.ToShortDateString();
			}
		}
		public static DateTime	SetDate(object date,LayerType layer)
		{
			if(date.ToString()=="" || date==DBNull.Value)
				return DateTime .MinValue;
			else 
				return Convert.ToDateTime(date);
		}

		public static int SetInt(object i,LayerType layer)
		{
			int tmp = 0;
			
			switch(layer)
			{
				case LayerType.PLL:
					if(i.ToString()=="" || i==null)
						tmp= 0;
					else 
						tmp =Convert.ToInt32(i);
					break;
					
				case LayerType.DAL:
					if(i.ToString()=="" || i==DBNull.Value)
						tmp= 0;
					else 
						tmp =Convert.ToInt32(i);
					break;
			}
			return tmp;
		}
		public static object	GetInt(int i,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.PLL:
					return i==0?(object)"":(object)i;
					
				case LayerType.DAL:
					return i==0?(object)DBNull.Value:(object)i;

				default:
					return i;
			}
		}

		public static string SetString(object s,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return s==DBNull.Value?"":s.ToString();

				case LayerType.PLL:
					if(s==null)
						return "";
					else 
						return s.ToString();
				
				default:
					return "";
			}
		}
		public static object GetString(string s,LayerType layer)
		{					
			switch(layer)
			{
				case LayerType.DAL:
					return(s=="" || s==null)?(object)DBNull.Value:s;
					
				case LayerType.PLL:
					return(s==null)?(object)"":s;
			
				default:
					return "";
			}
		}

		public static object GetBool(bool b,LayerType layer)
		{
			if( layer==LayerType.DAL)
				return b==false?0:1;
			
			if( layer==LayerType.PLL)
				return b==false?0:1;
			return b;
		}
		public static object GetBool(bool b,object TrueValue,object FalseValue, LayerType layer)
		{
			if( layer==LayerType.DAL)
				return b==false?0:1;
			
			if( layer==LayerType.PLL)
				return b==false?FalseValue:TrueValue;
			
			return b;
		}
		public static bool SetBool(object b,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return b==DBNull.Value?false:(bool)b;

				case LayerType.PLL:
					if(b.ToString()=="" || b==null)
						return false;
					else
					{
						//diffrentiate between "true" and 1;
						if(b is bool)
							return Convert.ToBoolean(b);
						else if(b is string)
							return Convert.ToBoolean(b.ToString());
						else
							return Convert.ToBoolean(Convert.ToInt32(b));
					}
					
				
				default:
					return false;
			}
		}
		public static object GetByte(byte b,LayerType layer)
		{
			if(layer==LayerType.DAL)
				return b==0?(object)DBNull.Value:b;
			return b;
		}
		public static byte SetByte(object b,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return b==DBNull.Value?(byte)0:(byte)b;

				case LayerType.PLL:
					if(b.ToString()=="" || b==null)
						return 0;
					else
						return Convert.ToByte(b);
				
				default:
					return 0;
			}
		}
		public static object GetByteArray(byte[] b,LayerType layer)
		{
			//TODO : Emplement this Method
			return null;
		}
		public static byte[] SetByteArray(object b,LayerType layer)
		{
			//			switch(layer)
			//			{
			//				case LayerType.DAL:
			//					return b==DBNull.Value?0:(byte[])b;
			//
			//				case LayerType.BLL:
			//					if(b.ToString()=="" || b==null)
			//						return 0;
			//					else
			//						return Convert.ToByte(b);
			//				
			//				default:
			//					return 0;
			//			}

			//TODO : Emplement this Method
			return null;
		}
		public static object GetGuid(System.Guid b,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return b==System.Guid.Empty?(object)DBNull.Value:b;

				case LayerType.BLL:
					return b==System.Guid.Empty?(object)"":b;
				
				default:
					return null;
			}
		}
		public static System.Guid SetGuid(object b,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return b==DBNull.Value?(System.Guid.Empty):(System.Guid)b;

				case LayerType.PLL:
					if(b.ToString()=="" || b==null)
						return System.Guid.Empty;
					else
						return (System.Guid)(b);
				
				default:
					return System.Guid.Empty;
			}
		}
		public static object GetObject(object obj,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return obj==null?(object)DBNull.Value:obj;

				case LayerType.PLL:
					return obj==null?(object)"":obj;
				
				default:
					return null;
			}
		}
		public static object SetObject(object obj,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return obj==DBNull.Value?null:(object)obj;

				case LayerType.PLL:
					if(obj.ToString()=="" || obj==null)
						return null;
					else
						return obj;
				
				default:
					return null;
			}
		}
		public static object GetDecimal(decimal d,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return d==decimal.MinValue?(object)DBNull.Value:d;

				case LayerType.PLL:
					return d==decimal.MinValue?(object)"":d;
				
				default:
					return null;
			}
		}
		public static decimal SetDecimal(object d,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return d==DBNull.Value?(decimal.MinValue):(decimal)d;

				case LayerType.PLL:
					if(d.ToString()=="" || d==null)
						return decimal.MinValue;
					else
						return Convert.ToDecimal(d);
				
				default:
					return decimal.MinValue;
			}
		}
		public static object GetDouble(double d,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return d==double.MinValue?(object)DBNull.Value:d;

				case LayerType.PLL:
					return d==double.MinValue?(object)"":d;
				
				default:
					return null;
			}
		}
		public static double SetDouble(object d,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return d==DBNull.Value?double.MinValue:(double)d;

				case LayerType.PLL:
					if(d.ToString()=="" || d==null)
						return double.MinValue;
					else
						return Convert.ToDouble(d);
				
				default:
					return double.MinValue;
			}
		}
		public static object GetFloat(float d,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return d==float.MinValue?(object)DBNull.Value:d;

				case LayerType.PLL:
					return d==float.MinValue?(object)"":d;
				
				default:
					return null;
			}
		}
		public static float SetFloat(object f,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return f==DBNull.Value?float.MinValue:(float)f;

				case LayerType.PLL:
					if(f.ToString()=="" || f==null)
						return float.MinValue;
					else
						return Convert.ToSingle(f);
				
				default:
					return float.MinValue;
			}
		}
		public static object GetLong(long L,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return L==long.MinValue?(object)DBNull.Value:L;

				case LayerType.PLL:
					return L==long.MinValue?(object)"":L;
				
				default:
					return null;
			}
		}
		public static long SetLong(object L,LayerType layer)
		{
			switch(layer)
			{
				case LayerType.DAL:
					return L==DBNull.Value?long.MinValue:(long)L;

				case LayerType.PLL:
					if(L.ToString()=="" || L==null)
						return 0;
					else
						return long.Parse(L.ToString());
				
				default:
					return long.MinValue;
			}
		}
		public abstract object OId
		{
			get;
			set;
		}
		public abstract object Text
		{
			get;

		}
	}
	}

